﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using System.Runtime.InteropServices;
 

namespace SH_VBoxServiceTrayApp
{

    /// -------------------------------------------------------------------------------------------------
    /// <summary> 
    /// Application Running Helper. 
    /// Helps me determine if the Program is running, if it is just activate it, 
    /// and ignore the execution of a new one.
    /// </summary>
    /// -------------------------------------------------------------------------------------------------
    class ApplicationRunningHelper
    {

        //Methods that help manipulater User GUI.
        [DllImport("user32.dll")]
        private static extern
            bool SetForegroundWindow(IntPtr hWnd);
        [DllImport("user32.dll")]
        private static extern
            bool ShowWindowAsync(IntPtr hWnd, int nCmdShow);
        [DllImport("user32.dll")]
        private static extern
            bool IsIconic(IntPtr hWnd);


        private ApplicationRunningHelper() { }


        /// -------------------------------------------------------------------------------------------------
        /// <summary> 
        /// check if current process already running. if running, set focus to existing process and 
        /// returns <see langword="true"/> otherwise returns <see langword="false"/>. 
        /// </summary>
        /// <returns> <see langword="true"/> if it succeeds, <see langword="false"/> if it fails. </returns>
        /// -------------------------------------------------------------------------------------------------
        public static bool AlreadyRunning()
        {
            //Flags of WindowsState
            /*
            const int SW_HIDE = 0;
            const int SW_SHOWNORMAL = 1;
            const int SW_SHOWMINIMIZED = 2;
            const int SW_SHOWMAXIMIZED = 3;
            const int SW_SHOWNOACTIVATE = 4;
            const int SW_RESTORE = 9;
            const int SW_SHOWDEFAULT = 10;
            */
            //const int swRestore = 9;
            const int showmaximized = 3;
            

            var me = Process.GetCurrentProcess();
            var arrProcesses = Process.GetProcessesByName(me.ProcessName); //If process was already created.

            if (arrProcesses.Length > 1)
            {
                for (var i = 0; i < arrProcesses.Length; i++)
                {
                    if (arrProcesses[i].Id != me.Id)
                    {
                        // get the window handle
                        IntPtr hWnd = arrProcesses[i].MainWindowHandle;

                        // if iconic, we need to restore the window
                        if (IsIconic(hWnd))
                        {
                            ShowWindowAsync(hWnd, showmaximized);
                        }

                        // bring it to the foreground
                        SetForegroundWindow(hWnd);
                        
                        break;
                    }
                }


                return true;
            }

            return false;
        }

    }



}


